package com.example.client.controller;

import com.example.client.Security.LoginUserDetails;
import com.example.client.pojo.Code;
import com.example.client.pojo.Result;
import com.example.client.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/client/users")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private HttpSession httpSession;

    @PostMapping("/login")
    public Result<Map> Login(String username, String password) {
        Result<Map> result = userService.login(username, password);
        if (result.getCode() == Code.RESPONSE_CODE_SUCCESS) {
            httpSession.setAttribute("username", username);
        }
        return result;
    }

    public void logout() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUserDetails loginUserDetails = (LoginUserDetails) authentication.getPrincipal();
        //TODO redis实现注销
        return;
    }
}
